// Load the http module to create an http server.
var http = require('http'),
path = require('path'),
fs = require('fs');
http.createServer(requestHandler).listen(8080);
function requestHandler(req, res) {
    var
    fileName = path.basename(req.url) || 'index.html',
    localFolder = __dirname + '/public/',
    page404 = localFolder + '404.html';
    getFile((localFolder + fileName),res,page404);
};
function getFile(filePath,res,page404){
    fs.exists(filePath,function(exists){
        if(exists){
            fs.readFile(filePath,function(err,contents){
                if(!err){
                    res.end(contents);
                } else {
                    console.dir(err);
                };
            });
        } else {
            fs.readFile(page404,function(err,contents){
                if(!err){
                    res.writeHead(404, {'Content-Type': 'text/html'});
                    res.end(contents);
                } else {
                    console.dir(err);
                };
            });
        };
    });
};